package com.genghis.ptas.friend.service.impl;

import com.genghis.ptas.constant.DictConstant;
import com.genghis.ptas.friend.dao.FriendDao;
import com.genghis.ptas.friend.entity.Friend;
import com.genghis.ptas.friend.entity.FriendChatRecord;
import com.genghis.ptas.friend.entity.FriendGroup;
import com.genghis.ptas.friend.service.FriendService;
import com.genghis.ptas.inbox.entity.Inbox;
import com.genghis.ptas.inbox.service.InboxService;
import com.genghis.ptas.security.dao.UserDao;
import com.genghis.ptas.security.entity.User;
import com.genghis.ptas.security.entity.UserBaseInfo;
import com.genghis.ptas.system.service.DictService;
import com.genghis.ptas.util.DateUtil;
import com.genghis.ptas.util.LoginUserUtil;
import com.genghis.ptas.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: gaoyang
 * To change this template use File | Settings | File Templates.
 */
@Service("friendService")
public class FriendServiceImpl implements FriendService {
    @Autowired
    private FriendDao friendDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private DictService dictService;

    @Autowired
    private InboxService inboxService;

    @Override
    public void addFriend(Friend friend) {
        Inbox inbox = new Inbox();
        inbox.setUserId(friend.getFriendId());
        inbox.setContent(friend.getUser().getUserName() + "申请加你为好友");
        inbox.setState(DictConstant.INBOX_STATE_2);
        inbox.setCategory(DictConstant.INBOX_CATEGORY_1);
        inbox.setAddresseeId(friend.getUser().getId());
        friend.setInboxId(inboxService.addInbox(inbox));
        friend.setState(DictConstant.VOID_VALUE);
        friend.setUserId(friend.getUser().getId());
        friendDao.addFriend(friend);
    }

    @Override
    public void delFriend(String ids) {
        friendDao.delFriend(ids);
    }

    @Override
    public Friend findFriendById(int userId) {
        return friendDao.findFriendById(userId);
    }

    @Override
    public void addFriendInfo(Friend friend) {
        friendDao.addFriendInfo(friend);
    }

    @Override
    public void editFriendInfoByUserId(Friend friend) {
        friendDao.editFriendInfoByUserId(friend);
    }

    @Override
    public List<User> findFriends(Friend friend) {
        String loginName = LoginUserUtil.findLoginUserName();
        int userId = userDao.findUserByLoginName(loginName).getId();
        friend.setUserId(userId);
        String friendName = StringUtil.iso2utf(friend.getFriendName());
        friend.setFriendName(friendName);
        List<User> userList = friendDao.findFriends(friend);
        for (int i = 0, len = userList.size(); i < len; i++) {
            if (userId == userList.get(i).getId()) {
                userList.remove(userList.get(i));
                break;
            }
        }
        for (User user : userList) {
            UserBaseInfo userBaseInfo = userDao.findUserBaseInfoByUserId(user.getId());
            userBaseInfo.setShowSex(dictService.changeDictCodeToValue(userBaseInfo.getSex(), DictConstant.SEX));
            user.setUserBaseInfo(userBaseInfo);
        }
        return userList;
    }

    private void addApplyReply(Friend friend) {
        User user = friend.getUser();
        Inbox inbox = new Inbox();
        inbox.setSendDate(DateUtil.getSystemDate(DictConstant.DATE_FORMAT));
        inbox.setCategory(DictConstant.INBOX_CATEGORY_1);
        inbox.setUserId(friend.getUserId());
        inbox.setAddresseeId(user.getId());
        StringBuilder message = new StringBuilder(user.getUserName());
        switch (friend.getState()) {
            case 1:
                message.append("已将您添加为好友！");
                break;
            case 2:
                message.append("拒绝将您添加为好友！");
                break;
        }
        inbox.setContent(message.toString());
        inbox.setState(DictConstant.VOID_VALUE);
        inboxService.addInbox(inbox);
    }

    private void addFriendGroup(Friend friend) {
        FriendGroup friendGroup = new FriendGroup(1);
        int id = friendDao.addFriendGroup(friendGroup);
        FriendGroup friendGroupUser = new FriendGroup(id, friend.getUserId(), DictConstant.STATE_IN_USE);
        friendDao.addFriendGroupUser(friendGroupUser);
        friendGroupUser = new FriendGroup(id, friend.getFriendId(), DictConstant.STATE_IN_USE);
        friendDao.addFriendGroupUser(friendGroupUser);
    }

    @Override
    public void changeApplyState(Friend friend) {
        friendDao.changeApplyState(friend);
        if (friend.getState() == 1) {
            addFriendGroup(friend);
        }
        addApplyReply(friend);
    }

    @Override
    public List<Friend> listAllFriend(HttpSession session, Friend friend) {
        User user = (User) session.getAttribute("user");

        ServletContext application = session.getServletContext();
        List onlineUserList = (List) application.getAttribute("onlineUserList");

        friend.setUserId(user.getId());
        List<Friend> friendList = friendDao.listAllFriend(friend);
        for (Friend friendTemp : friendList) {
            int friendId = friendTemp.getFriendId();
            User userTemp = userDao.findUserAllInfoById(friendId);
            UserBaseInfo userBaseInfo = userDao.findUserBaseInfoByUserId(friendId);
            userTemp.setUserBaseInfo(userBaseInfo);
            friendTemp.setUser(userTemp);
            int state = onlineUserList.contains(userTemp.getLoginName()) ? DictConstant.ON_LINE : DictConstant.NOT_ON_LINE;
            friendTemp.setState(state);
            friendTemp.setShowOnLine(dictService.changeDictCodeToValue(state, DictConstant.IS_ON_LINE));
        }
        return friendList;
    }

    @Override
    public int addFriendGroup(FriendGroup friendGroup) {
        return friendDao.addFriendGroup(friendGroup);
    }

    @Override
    public void addFriendGroupUser(FriendGroup friendGroup) {
        friendDao.addFriendGroupUser(friendGroup);
    }

    @Override
    public void addChatRecord(FriendChatRecord friendChatRecord) {
        friendChatRecord.setSendDate(DateUtil.getSystemDate(DictConstant.DATE_FORMAT));
        friendDao.addChatRecord(friendChatRecord);
    }

    @Override
    public int findFriendGroupId(Friend friend) {
        return friendDao.findFriendGroupId(friend);
    }

    @Override
    public List<FriendChatRecord> findChatRecord(FriendChatRecord friendChatRecord) {
        return friendDao.findChatRecord(friendChatRecord);
    }
}
